package com.peace.hospital.opdept.controller.outpatientdept;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.peace.hospital.entity.common.People;
import com.peace.hospital.entity.common.ResponseBean;
import com.peace.hospital.entity.outpatientdept.Allcharge;
import com.peace.hospital.entity.outpatientdept.Drugcharges;
import com.peace.hospital.entity.outpatientdept.Inpatientmessage;
import com.peace.hospital.entity.outpatientdept.Patientinformation;
import com.peace.hospital.entity.outpatientdept.dto.ConsumptionDetails;
import com.peace.hospital.opdept.service.common.IPeopleService;
import com.peace.hospital.opdept.service.outpatientdept.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收费详情表 前端控制器
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-11-11
 */
@RestController
@RequestMapping("/particulars")
public class ParticularsController {
    @Autowired
    private IPeopleService iPeopleService;
    /**挂号信息表*/
    @Autowired
    private IPatientinformationService iPatientinformationService;
    /**挂号处方表*/
    @Autowired
    private IChargingitemsService iChargingitemsService;
    /**药品处方表*/
    @Autowired
    private IDrugchargesService iDrugchargesService;
    /**住院处方表*/
    @Autowired
    private IInpatientmessageService iInpatientmessageService;
    /**总收费表*/
    @Autowired
    private IAllchargeService iAllchargeService;
    /**
     * 通过病人姓名&身份证号查询消费信息计算总消费表
     */
    @RequestMapping(value = "/select", method = RequestMethod.GET,consumes="application/json")
    public String findConsumptionDetailsByNameAndCard(@RequestBody People people) {
        List<ConsumptionDetails> consumptionDetails = new ArrayList<>();
        //根据用户名或者身份证查询病人信息
        List<People> peoples = iPeopleService.getPeopleByNameOrCode(people);
        for (People people1 : peoples) {
            StringBuilder sb = new StringBuilder(people1.getPcode());
            sb.replace(6, 10, "****");
            people1.setPcode(sb.toString());
            Patientinformation patientinformation = iPatientinformationService.selectPatientinformationByPid(people1.getPid());
            ConsumptionDetails consumptionDetails1 = new ConsumptionDetails(people1, patientinformation);
            //查询处方检查收费信息
            consumptionDetails1.setChargingitems(iChargingitemsService.selectChargingitemsByPid(people1.getPid()));
            //查询处方检查收费金额
            consumptionDetails1.setChargprice(iChargingitemsService.selectChargpriceByPid(people1.getPid()));
            //查询药品收费信息
            consumptionDetails1.setDrugcharges(iDrugchargesService.selectDrugchargesByPid(people1.getPid()));
            //查询药品收费金额
            consumptionDetails1.setDrugprice(iDrugchargesService.selectDrugpricesByPid(people1.getPid()));
            //查询住院收费信息
            consumptionDetails1.setInpatientmessage(iInpatientmessageService.selectInpatientmessageByPid(people1.getPid()));
            //查询住院收费金额
            consumptionDetails1.setInpatientprice(iInpatientmessageService.selectInpatientpriceeByPid(people1.getPid()));
            //查询病人挂号日期信息
            consumptionDetails1.setPatientinformation(patientinformation);
            //录入总收费表
            Allcharge allcharge = new Allcharge();
            allcharge.setPid(people1.getPid());
            //计算出总价格
            Double allCount = consumptionDetails1.getChargprice()+consumptionDetails1.getDrugprice()+consumptionDetails1.getInpatientprice();
            allcharge.setAllprice(allCount);
            allcharge.setAllsettlement(false);
            //录入总收费表
            iAllchargeService.addAllchargeOne(allcharge);
            consumptionDetails1.setTotalCharge(allCount);
            consumptionDetails.add(consumptionDetails1);
        }
        System.out.println(consumptionDetails);
        return JSON.toJSONStringWithDateFormat(new ResponseBean(200,"success",consumptionDetails),"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
    }


    /**
     * 通过病人ID结算
     */
    @RequestMapping(value = "/settlement", method = RequestMethod.GET)
    public String peopleSettlementById(@RequestBody People people) {
        return iAllchargeService.peopleSettlementById(people);
    }
}
